from rest_framework import serializers

from apps.found_person.models import Recruit
from apps.news.models import Article
from apps.answer.models import Question, Label, Reply
from apps.users.models import User


class LabelSerializers(serializers.ModelSerializer):
    class Meta:
        model=Label
        fields='__all__'


class UserReplySerializer(serializers.ModelSerializer):
    class Meta:
        model=User
        fields = ('id','username','avatar')
        # username字段增加长度限制，password字段只参与保存，不在返回给前端，增加write_only选项参数
        extra_kwargs = {
            'username': {
                'max_length': 20,
                'min_length': 5
            },
            'password': {
                'max_length': 20,
                'min_length': 8,
                'write_only': True
            },
        }


class ReplySerializer(serializers.ModelSerializer):

    class Meta:
        model=Reply
        fields='__all__'


class CreatReplyView(serializers.ModelSerializer):
    class Meta:
        model=Reply
        fields='__all__'


class QuestionSerializers(serializers.ModelSerializer):
    labels=LabelSerializers(many=True)
    subs=serializers.SerializerMethodField()
    class Meta:
        model=Question
        fields='__all__'




# 对回答的评论
class SubCommentSerializer(serializers.ModelSerializer):
    user=UserReplySerializer('用户')
    class Meta:
        model=Reply
        fields='__all__'



class AnswerQuestionSerializer(serializers.ModelSerializer):
    subs=serializers.SerializerMethodField()
    user=UserReplySerializer('用户')

    class Meta:
        model=Reply
        fields='__all__'

    def get_subs(self, obj):
        subs=obj.subs.filter(parent__isnull=False,type=1)
        return SubCommentSerializer(subs,many=True,read_only=True).data



class CommentQuestionSerializer(serializers.ModelSerializer):
    subs=serializers.StringRelatedField()
    user=UserReplySerializer(label='用户')

    class Meta:
            model=Reply
            fields='__all__'



class QuestionSerializer(serializers.ModelSerializer):
    labels=serializers.StringRelatedField(many=True,read_only=True)
    user=serializers.StringRelatedField(label='用户')
    # replies=ReplySerializer(many=True,read_only=True)
    comment_question=serializers.SerializerMethodField(label='评论问题')
    answer_question=serializers.SerializerMethodField(label='回答问题')

    def get_user(self, obj):
        # 自定义获取用户字段的逻辑
        # 假设你的User模型有一个名为avatar的ImageField字段
        if obj.user.avatar:
            return {
                'username': obj.user.username,
                'avatar': 'http://s7g9jujrf.hn-bkt.clouddn.com/' + obj.user.avatar
            }
        else:
            return {
                'username': obj.user.username,
                'avatar': None
            }
    def get_label_icon(self, obj):
        # 自定义获取图片字段的逻辑
        # 这里假设你的Label模型有一个名为label_icon的ImageField字段
        if obj.label_icon:
            return 'http://s7g9jujrf.hn-bkt.clouddn.com/' + obj.label_icon.url
        else:
            return None
    def get_comment_question(self,obj):
        replies= obj.replies.filter(parent__isnull=True,type=0)
        return CommentQuestionSerializer(replies,many=True,read_only=True).data

    def get_answer_question(self, obj):
        replies = obj.replies.filter(parent__isnull=True, type=2)
        return AnswerQuestionSerializer(replies, many=True,read_only=True).data

    class Meta:
        model=Question
        fields=["id","createtime","labels","reply","replyname","replytime","title",
                  "unuseful_count","useful_count","user","visits","content",
                  "comment_question","answer_question"]



class QuestionViewSerializer(serializers.ModelSerializer):
    labels = LabelSerializers(many=True)
    user=serializers.StringRelatedField(label='用户')
    class Meta:
        model=Question
        fields='__all__'


class ArticlesViewSerializer(serializers.ModelSerializer):
    class Meta:
        model=Article
        fields='__all__'

class RecruitsViewSerializer(serializers.ModelSerializer):
    class Meta:
        model=Recruit
        fields='__all__'


from rest_framework import serializers

class LabelSelizerial(serializers.ModelSerializer):
    questions = QuestionViewSerializer(many=True)
    articles = ArticlesViewSerializer(many=True)
    label_icon = serializers.SerializerMethodField(label='source')
    def get_label_icon(self, obj):
        # 自定义获取图片字段的逻辑
        # 这里假设你的Label模型有一个名为label_icon的ImageField字段
        if obj.label_icon:
            return 'http://s7g9jujrf.hn-bkt.clouddn.com/' + obj.label_icon.url
        else:
            return None
    class Meta:
        model = Label
        fields = '__all__'